// import client from "./client"
// 注册


import {basePath} from "./client.js";

export const createUser = async (userInfo) => {
    try {
        const {data} = await client.post('/user/create', userInfo)
        return data
    } catch (e) {
        const {response} = e
        if (response?.data) return {error: response.data.error}
        return {error: e.message || e}
    }
}
// 验证邮箱
export const verifyUserEmail = async (userInfo) => {
    try {
        const {data} = await client.post('/user/verify-email', userInfo)
        return data
    } catch (e) {
        const {response} = e
        console.log(e);
        if (response?.data) return {error: response.data.error}
        return {error: e.message || e}
    }
}
// 登录
export const signInUser = async (userInfo) => {
    try {
        const res = await fetch(basePath + '/common/login',
            {
                //请求的body信息 如：Blob、BufferSource、FormData、URLSearchParams 或者 USVString 对象
                //这里必须匹配 'Content-Type' //注意 GET 或 HEAD 方法的请求不能包含 body 信息。
                body: JSON.stringify(userInfo),
                //请求的头信息
                headers: {
                    'content-type': 'application/json'
                },
                //请求使用的方法  //如：GET, POST, PUT, DELETE等
                method: 'POST',
                //请求的模式	 //如 cors、 no-cors 或者 same-origin。
                mode: 'cors'
            })
        return await res.json()
    } catch (e) {
        const {response} = e
        if (response?.data.error) return {error: response.data.error}
        return {error: e.message || e}
    }
}
// 认证token
export const getIsAuth = async (token) => {
    try {
        const res = await fetch(basePath+'/common/is-auth', {
            method: 'get',
            headers: {
                Authorization: "Bearer " + token,
                Accept: "application/json"
            }
        })
        return await res.json()
    } catch (e) {
        const {response} = e
        console.log(response?.data.error);
        if (response?.data.error) return {error: response.data.error}
        return {error: e.message || e}
    }
}
// 忘记密码 
export const forgetPassword = async (email) => {
    try {
        const {data} = await client.post('/user/forget-password', {email})
        return data
    } catch (e) {
        const {response} = e
        console.log(response?.data.error);
        if (response?.data.error) return {error: response.data.error}
        return {error: e.message || e}
    }
}

// 验证token: 邮箱点击重置密码后携带token和id跳转时的验证
export const verifyPasswordResetToken = async (token, userId) => {
    try {
        const {data} = await client.post('/user/verify-pass-reset-token', {token, userId})
        return data
    } catch (e) {
        const {response} = e
        console.log(response?.data.error);
        if (response?.data.error) return {error: response.data.error}
        return {error: e.message || e}
    }
}

// 重置密码 
export const resetPassword = async (passwordInfo) => {
    try {
        const {data} = await client.post('/user/reset-password', passwordInfo)
        return data
    } catch (e) {
        const {response} = e
        console.log(response?.data.error);
        if (response?.data.error) return {error: response.data.error}
        return {error: e.message || e}
    }
}
// 重新发送OTP 
export const resendEmailVerificationToken = async (userId) => {
    try {
        const {data} = await client.post('/user/resend-email-verification-token', {userId})
        return data
    } catch (e) {
        const {response} = e
        console.log(response?.data.error);
        if (response?.data.error) return {error: response.data.error}
        return {error: e.message || e}
    }
}